/**
 * 
 */
var catagory = new Object({
	URL : {
		info:"/catagory/",
		add : "/catagory/add"
	},
	add : function() {
		var name = $("#catagoryName").val();
		var desc = $("#catagoryDesc").val();
		$.post(catagory.URL.add, {
			"name" : name,
			"desc" : desc
		}, function(data) {
			if (data.success) {
				$("#tipsAlert").removeClass("alert-danger").addClass("alert-success");
				$("#tipsAlert strong").html(data.error);
				$("#tipsAlert").fadeIn(1000,function(){
					location.href = catagory.URL.info;
				});
			} else {
				$("#tipsAlert").removeClass("alert-success").addClass("alert-danger");
				$("#tipsAlert strong").html(data.error);
				$("#tipsAlert").show();
			}
		});
	},

});

var fragment = new Object({
	URL : {
		add : "/fragment/add",
		content:"/fragment/getContent"
	},
	add : function() {
		var title = $("#fragment_title").val();
		var rowcontent = $("#fragment_content").val();
		var catagoryId = $("#fragment_catagoryId").val();
		$.post(fragment.URL.add,{"title":title,"catagoryId":catagoryId,"content":rowcontent},function(data){
			if(data.success){
				$("#tipsAlert").removeClass("alert-danger").addClass("alert-success");
				$("#tipsAlert strong").html(data.error);
				$("#tipsAlert").fadeIn(1000,function(){
					location.href = catagory.URL.info+catagoryId;
				});
			}else{
				$("#tipsAlert").removeClass("alert-success").addClass("alert-danger");
				$("#tipsAlert strong").html(data.error);
				$("#tipsAlert").show();
			}
		});
	},
	
	preview:function(){
		var title = $("#fragment_title").val();
		var rowcontent = $("#fragment_content").val();
		rowcontent = utils.unhtml(rowcontent);
		$("#preview_title").html(title);
		$("#preview_content").html(rowcontent);
		$("#previewFragment").modal();
	},
	showContent:function(id){
		$.post(fragment.URL.content,{"fragmentId":id},function(data){
			if(data.success){
				$("#content_fragment"+id).html(utils.unhtml(data.data));
				$("#Fragment"+id).collapse('toggle');
			}
		});
	}
});

var utils = new Object({
	unhtml : function(str){
		return str ? str.replace(/[<">']/g, (a) => {
            return {
                '<': '&lt;',
                '"': '&quot;',
                '>': '&gt;',
                "'": '&#39;'
            }[a]
        }) : '';
	}

});